package ups.glre.news.struts.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import ups.glre.news.model.Category;
import ups.glre.news.model.User;
import ups.glre.news.service.CategoryService;
import ups.glre.news.service.UserService;
import ups.glre.news.struts.form.SubscriptionForm;

public class SubscriptionAction extends DispatchAction{
	
	private CategoryService categoryService;
	private UserService userService;

	public SubscriptionAction() {
		InitialContext ic = null;
		try {
			ic = new InitialContext();
			categoryService = (CategoryService) ic.lookup("glre-news/CategoryServiceImpl/local");
			userService = (UserService) ic.lookup("glre-news/UserServiceImpl/local");
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public ActionForward update(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response) 
			throws IOException, ServletException {
		
		SubscriptionForm subscriptionForm = (SubscriptionForm)form;
		
		User user = ((User) request.getSession().getAttribute("user"));
		int userId = user.getUserId();
		
		String[] selectedCategories = subscriptionForm.getSelectedCategories();
		
		List<Integer> categoryToSubscribe = new ArrayList<Integer>();
		List<Integer> categoryToUnSubscribe = new ArrayList<Integer>();
		
		Category c = new Category();
		List<Category>subscribedCategories = subscriptionForm.getSubscribedCategories();
		
		System.out.println("subscribed categories : " + subscribedCategories.toString());
		
		for (int i = 0; i< selectedCategories.length;i++){
			int categoryId = Integer.parseInt(selectedCategories[i]);
			c.setCategoryId(categoryId);
			if (!subscribedCategories.contains(c)){
				categoryToSubscribe.add(categoryId);
			}
		}
		
		System.out.println("categories to subscribe : " + categoryToSubscribe.toString());
		
		for (Category category : subscribedCategories){
			int categoryId = category.getCategoryId();
			boolean find = false;
			
			for (int i = 0; i<selectedCategories.length;i++){				
				if (categoryId == Integer.parseInt(selectedCategories[i])){
					find = true;
				}
			}
			
			if (!find){
				categoryToUnSubscribe.add(categoryId);
			}
		}				
		
		
		System.out.println("categories to unSubscribe : " + categoryToUnSubscribe.toString());
		
		userService.unSubscribeUserFromCategory(userId, categoryToUnSubscribe);
		user = userService.subscribeUserToCategory(userId, categoryToSubscribe);
		
		request.getSession().setAttribute("user", user);
		
		subscribedCategories = new ArrayList<Category>(user.getSubscribedCategories());
				
		subscriptionForm.setSubscribedCategories(subscribedCategories);
		
		String forwardTo="succes";
		return mapping.findForward(forwardTo);
	}
	
	public ActionForward list(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response) 
			throws IOException, ServletException {
		
		User user = ((User) request.getSession().getAttribute("user"));
		List<Category> subscribedCategories = new ArrayList<Category>(user.getSubscribedCategories()); 
		
		System.out.println(" subscribedCategories size : " +subscribedCategories.size());
		System.out.println(" subscribedCategories : " +subscribedCategories.toString());
		
		System.out.println(" user.getSubscribedCategories() size : " +user.getSubscribedCategories().size());
		
		
		SubscriptionForm subscriptionForm = (SubscriptionForm)form;
		subscriptionForm.setCategories(categoryService.getAllCategory());
		subscriptionForm.setSubscribedCategories(subscribedCategories);
		
		String forwardTo="succes";
		return mapping.findForward(forwardTo);
	}
	
}
